package com.iptv.common.utils;

import com.iptv.common.core.domain.AjaxResult;
import com.iptv.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RegistrationTokenUtils {

    @Autowired
    private RedisCache redisCache;

    public String getRegistrationToken(String mac) {
        // 生成唯一的注册令牌
        long timestamp = System.currentTimeMillis();
        String registrationToken = mac + ":" + timestamp;
        redisCache.setCacheObject("registrationToken:" + mac, registrationToken);
        return registrationToken;
    }

    public AjaxResult checkToken(String token) {
        if (token == null) {
            return AjaxResult.error("缺少注册令牌");
        }
        String mac = token.split(":")[0]; // 提取唯一mac

        String storedToken = redisCache.getCacheObject("registrationToken:" + mac);
        if (storedToken == null || !storedToken.equals(token)) {
            return AjaxResult.error("无效的注册令牌");
        }

        return null;
    }

    public String checkTokenExist(String mac) {
        // mac获取token
        return redisCache.getCacheObject("registrationToken:" + mac);
    }

}
